﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using DevExpress.XtraCharts;
using DevExpress.XtraPivotGrid;

namespace ReportingManager
{
    public partial class frmRaporGoruntule : DevExpress.XtraEditors.XtraForm
    {
        public object DataSource { get; set; }
        List<TableInfo> tableInfo = new List<TableInfo>();
        ExListItem items = null;

        public frmRaporGoruntule()
        {
            InitializeComponent();
            DevExpress.Skins.SkinManager.EnableFormSkins();
        }

        #region Method
        void CreateTableInfo()
        {
            if (File.Exists(Config.Get.Path + "\\" + Config.Get.RaporAdi + ".xml"))
            {
                DataSet ds = new DataSet();
                ds.ReadXml(Config.Get.Path + "\\" + Config.Get.RaporAdi + ".xml");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    tableInfo.Add(new TableInfo() { Kolon_Adi = ds.Tables[0].Rows[i][0].ToString(), Filtre_Uygula = Convert.ToBoolean(ds.Tables[0].Rows[i][1]), Datada_Gor = Convert.ToBoolean(ds.Tables[0].Rows[i][2]), Kolonda_Gor = Convert.ToBoolean(ds.Tables[0].Rows[i][3]), Hicbiri = Convert.ToBoolean(ds.Tables[0].Rows[i][4]), Satirda_Gor = Convert.ToBoolean(ds.Tables[0].Rows[i][5]), GroupInterval = (PivotGroupInterval)Convert.ToInt32(ds.Tables[0].Rows[i][6]), DbType = Type.GetType(ds.Tables[0].Rows[i][7].ToString()) });
                }
            }
        }
        void CreateField()
        {
            try
            {
                int sayac = 1;
                for (int i = 0; i < tableInfo.Count; i++)
                {
                    if (tableInfo[i].Hicbiri)
                        continue;

                    if (tableInfo[i].Datada_Gor)
                    {
                        DevExpress.XtraPivotGrid.PivotGridField tableField = new DevExpress.XtraPivotGrid.PivotGridField();
                        tableField.Area = DevExpress.XtraPivotGrid.PivotArea.DataArea;
                        tableField.GroupInterval = tableInfo[i].GroupInterval;
                        tableField.AreaIndex = sayac;
                        tableField.Name = "field" + tableInfo[i].Kolon_Adi + sayac;
                        tableField.FieldName = tableInfo[i].Kolon_Adi;
                        switch (tableInfo[i].DbType.FullName)
                        {
                            case "System.Int64":
                            case "System.Int16":
                            case "System.Int32":
                                tableField.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Average;
                                break;

                            case "System.Double":
                            case "System.Decimal":
                                tableField.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Sum;
                                break;
                            default:
                                tableField.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Count;
                                break;
                        }
                        pvtGridRapor.Fields.Add(tableField);
                        sayac++;
                    }
                    if (tableInfo[i].Filtre_Uygula)
                    {
                        DevExpress.XtraPivotGrid.PivotGridField tableField = new DevExpress.XtraPivotGrid.PivotGridField();
                        tableField.Area = DevExpress.XtraPivotGrid.PivotArea.FilterArea;
                        tableField.AreaIndex = sayac;
                        tableField.GroupInterval = tableInfo[i].GroupInterval;
                        tableField.Name = "field" + tableInfo[i].Kolon_Adi + sayac;
                        tableField.FieldName = tableInfo[i].Kolon_Adi;
                        pvtGridRapor.Fields.Add(tableField);
                        sayac++;
                    }
                    if (tableInfo[i].Kolonda_Gor)
                    {
                        DevExpress.XtraPivotGrid.PivotGridField tableField = new DevExpress.XtraPivotGrid.PivotGridField();
                        tableField.Area = DevExpress.XtraPivotGrid.PivotArea.ColumnArea;
                        tableField.AreaIndex = sayac;
                        tableField.Name = "field" + tableInfo[i].Kolon_Adi + sayac;
                        tableField.FieldName = tableInfo[i].Kolon_Adi;
                        pvtGridRapor.Fields.Add(tableField);
                        tableField.GroupInterval = tableInfo[i].GroupInterval;
                        sayac++;
                    }
                    if (tableInfo[i].Satirda_Gor)
                    {
                        DevExpress.XtraPivotGrid.PivotGridField tableField = new DevExpress.XtraPivotGrid.PivotGridField();
                        tableField.Area = DevExpress.XtraPivotGrid.PivotArea.RowArea;
                        tableField.AreaIndex = sayac;
                        tableField.Name = "field" + tableInfo[i].Kolon_Adi + sayac;
                        tableField.FieldName = tableInfo[i].Kolon_Adi;
                        tableField.GroupInterval = tableInfo[i].GroupInterval;
                        pvtGridRapor.Fields.Add(tableField);
                        sayac++;
                    }

                }
            }
            catch (Exception _ex)
            {
                MessageBox.Show(_ex.Message);
            }

        }
        void LoadSource()
        {
            pvtGridRapor.DataSource = this.DataSource;
        }
        void ViewAta(object sender)
        {
            try
            {
                //ExListView lst = (ExListView)sender;
                //items = lst.SelectedItems[0] as ExListItem;
                //DevExpress.XtraCharts.SeriesViewBase view = (DevExpress.XtraCharts.SeriesViewBase)lst.SelectedView.Clone();
                //chartControl1.SeriesTemplate.View = view;
                //if (items.Style == ExListItem.Gorunum.UcBoyutlu)
                //    if (items.Tip == ExListItem.ViewTip.Pie3D || items.Tip == ExListItem.ViewTip.Doughnut3D)
                //        ((SimpleDiagram3D)chartControl1.Diagram).RuntimeRotation = true;
                //    else
                //        ((XYDiagram3D)chartControl1.Diagram).RuntimeRotation = true;


                ExListView lst = (ExListView)sender;
                items = lst.SelectedItems[0] as ExListItem;
                SetView.GetSelectedView(items.Tip, chartControl1);
                if (items.Style == ExListItem.Gorunum.UcBoyutlu)
                    if (items.Tip == ExListItem.ViewTip.Pie3D || items.Tip == ExListItem.ViewTip.Doughnut3D)
                        ((SimpleDiagram3D)chartControl1.Diagram).RuntimeRotation = true;
                    else
                        ((XYDiagram3D)chartControl1.Diagram).RuntimeRotation = true;

            }
            catch (Exception _ex)
            {
                MessageBox.Show("Porgram Hatayla Karşılaştı :" + _ex.Message);
            }
        }
        #endregion

        private void frmRaporGoruntule_Load(object sender, EventArgs e)
        {
            this.Text = Config.Get.RaporAdi;
            lvItemGrafik.AddItem(this.chartControl1);
            CreateTableInfo();
            CreateField();
            LoadSource();
        }

        private void lvItemGrafik_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                ViewAta(sender);
            }
            catch (Exception _ex)
            {
                MessageBox.Show("Porgram Hatayla Karşılaştı :" + _ex.Message);
            }
        }

        private void lvItemGrafik_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                ViewAta(sender);
                xtraTabControl1.SelectedTabPageIndex = 2;
            }
            catch (Exception _ex)
            {
                MessageBox.Show("Porgram Hatayla Karşılaştı :" + _ex.Message);
            }
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (xtraTabControl1.SelectedTabPageIndex == 0)
                    pvtGridRapor.Print();
                else if (xtraTabControl1.SelectedTabPageIndex == 2)
                    chartControl1.Print();
            }
            catch (Exception _ex)
            {
                MessageBox.Show(_ex.Message);
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                using (SaveFileDialog dlg = new SaveFileDialog())
                {
                    dlg.DefaultExt = ".pdf";
                    dlg.Filter = "Pdf Dosyalar |*.pdf";
                    if (dlg.ShowDialog() == DialogResult.OK && dlg.FileName != string.Empty)
                    {
                        if (xtraTabControl1.SelectedTabPageIndex == 0)
                            pvtGridRapor.ExportToPdf(dlg.FileName);
                        else if (xtraTabControl1.SelectedTabPageIndex == 2)
                            chartControl1.ExportToPdf(dlg.FileName);
                    }
                }
            }
            catch (Exception _ex)
            {
                MessageBox.Show(_ex.Message);
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                using (SaveFileDialog dlg = new SaveFileDialog())
                {
                    dlg.DefaultExt = ".xls";
                    dlg.Filter = "Excel Dosyalar |*.xls";
                    if (dlg.ShowDialog() == DialogResult.OK && dlg.FileName != string.Empty)
                    {
                        if (xtraTabControl1.SelectedTabPageIndex == 0)
                            pvtGridRapor.ExportToXls(dlg.FileName);
                        else if (xtraTabControl1.SelectedTabPageIndex == 2)
                            chartControl1.ExportToXls(dlg.FileName);

                    }
                }
            }
            catch (Exception _ex)
            {
                MessageBox.Show(_ex.Message);
            }
        }
    }
}